
library(tidyverse) ## to use various functions
library(ggplot2) ## to generate plots in R in high resolution
library(ggpubr) ## to combine several plots in one figure
library(haven) ## to open datasets written in .dta (Stata) format


setwd("INSERT DIRECTORY PATH HERE")


### To replicate Figure 1 (main text), use the file "churches_1960_2019.csv"

### Figure 1: Main text (Number of registered evangelical churches in Brazil (1960-2019)

df_numb <- read.csv("churches_1960_2019.csv", sep =",", header = TRUE)

p1 <- ggplot(data=df_numb, aes(x=year, y=number, group=1)) +
  geom_path()+
  geom_point() + theme_minimal() + ylim(0, max(115000)) +
  scale_x_continuous(breaks=seq(1960, 2020, 10)) +
  ylab("Number of registered evangelical churches") + xlab("Year") 
p1



### To replicate Figure 2 (main text), use the file "df_LPT_igrejas_outcomes.dta"

## Figure 2: Main text (Outcome variables descriptive statistics)

df_panel <- read_dta("df_LPT_igrejas_outcomes.dta")


# Voter turnout
box_turnout <- ggplot(df_panel, aes(x = turnout)) + 
  geom_density(aes(fill = turnout), color="Navy", linewidth=0.2) +
  geom_boxplot(width = 0.2, color="Grey", linewidth=0.2) +
  theme_classic() +
  theme(legend.position = "none") +
  xlab("") + ylab("Density") +
  ggtitle("",
          subtitle = "Voter turnout")
box_turnout 


# Electoral competition
box_comp <- ggplot(df_panel, aes(x = comp)) + 
  geom_density(aes(fill = comp), color="Navy", linewidth=0.2) +
  geom_boxplot(width = 0.2, color="Grey", linewidth=0.2) +
  theme_classic() +
  theme(legend.position = "none") +
  xlab("") + ylab("") +
  ggtitle("",
          subtitle = "Electoral competition") 
box_comp 


# Electoral conservatism
box_conserv <- ggplot(df_panel, aes(x = ideo_imp)) + 
  geom_density(aes(fill = ideo_imp), color="Navy", linewidth=0.2) +
  geom_boxplot(width = 0.2, color="Grey", linewidth=0.2) +
  theme_classic() +
  theme(legend.position = "none") +
  xlab("") + ylab("Density") +
  ggtitle("",
          subtitle = "Electoral conservatism") 
box_conserv 


# Electoral polarisation
box_pol <- ggplot(df_panel, aes(x = pol_pi)) + 
  geom_density(aes(fill = pol_pi), color="Navy", linewidth=0.2) +
  geom_boxplot(width = 0.2, color="Grey", linewidth=0.2) +
  theme_classic() +
  theme(legend.position = "none") +
  xlab("") + ylab("") +
  ggtitle("",
          subtitle = "Electoral polarization") 
box_pol 


Figure_2comb <- ggarrange(box_turnout,box_comp,box_conserv,box_pol,  
                            labels = c("A", "B", "C", "D"),
                            ncol = 2, nrow = 2)
Figure_2comb



setwd("INSERT DIRECTORY PATH HERE")

### To replicate Figure 6 (main text), use the following files: 

     ## bw_sensitiveness_turnout.csv ##
     ## bw_sensitiveness_competition.csv ##
     ## bw_sensitiveness_conservatism.csv ##
     ## bw_sensitiveness_polarization.csv ##

## Figure 6: Main text (BW est. (h) sensitiveness of reduced form estimates)

df_bw <- read.csv("bw_sensitiveness_turnout.csv", sep =",", header = TRUE)
pturnout <- ggplot(df_bw, aes(bw_h, coef)) + 
  geom_point(colour = "black", fill = "white", size = 0.5, stroke = 0.5) + 
  theme_minimal() + ylab("Reduced form estimates") + xlab("") +
  ylim(-0.10, max(0.10)) + xlim(6.1, max(9.1)) +
  geom_hline(yintercept = 0, linetype="dotted",
             color="red") + geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.5) +
  ggtitle("",
          subtitle = "Voter turnout")
pturnout


df_bw <- read.csv("bw_sensitiveness_competition.csv", sep =",", header = TRUE)
pcomp <- ggplot(df_bw, aes(bw_h, coef)) + 
  geom_point(colour = "black", fill = "white", size = 0.5, stroke = 0.5) + 
  theme_minimal() + ylab("") + xlab("") +
  ylim(-0.10, max(0.10)) + xlim(3.1, max(6.2)) +
  geom_hline(yintercept = 0, linetype="dotted",
             color="red") + geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.5) +
  ggtitle("",
          subtitle = "Electoral competition")
pcomp



df_bw <- read.csv("bw_sensitiveness_conservatism.csv", sep =",", header = TRUE)
pconserv <- ggplot(df_bw, aes(bw_h, coef)) + 
  geom_point(colour = "black", fill = "white", size = 0.5, stroke = 0.5) + 
  theme_minimal() + ylab("Reduced form estimates") + xlab(" BW est. (h)") +
  ylim(-0.10, max(0.10)) + xlim(2.8, max(6)) +
  geom_hline(yintercept = 0, linetype="dotted",
             color="red") + geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.5) +
  ggtitle("",
          subtitle = "Electoral conservatism")
pconserv



df_bw <- read.csv("bw_sensitiveness_polarization.csv", sep =",", header = TRUE)
ppolari <- ggplot(df_bw, aes(bw_h, coef)) + 
  geom_point(colour = "black", fill = "white", size = 0.5, stroke = 0.5) + 
  theme_minimal() + ylab("") + xlab(" BW est. (h)") +
  ylim(-0.10, max(0.10)) + xlim(5, max(8.1)) +
  geom_hline(yintercept = 0, linetype="dotted",
             color="red") + geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.5) +
  ggtitle("",
          subtitle = "Electoral polarization")
ppolari 



Figure_6comb <- ggarrange(pturnout,pcomp,pconserv,ppolari,  
                            labels = c("A", "B", "C", "D"),
                            ncol = 2, nrow = 2)
Figure_6comb













